﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Strategy.Core.TradeTypes;

namespace Strategy.Core.TradeStreams.Indicators
{
    public abstract class AIndicator : ITradeStream<IndicatorValue>
    {
        
        protected abstract double getCalculatedValue(QuoteData qd);
        protected virtual bool isNewInterval(QuoteData qd)
        {

            if (values == null)
                values = new List<IndicatorValue>();
            if (values.Count == 0)
                return true;
            return qd.Date >= values.Last().CloseTime;
        }
        protected virtual void addValue(QuoteData qd, double calculatedValue)
        {

            if (values == null)
                values = new List<IndicatorValue>();
            if (isNewInterval(qd))
                values.Add(new IndicatorValue(qd.Date, Interval, calculatedValue));
            else
                values[values.Count - 1].Value = calculatedValue;
        }
        protected virtual void validate(QuoteData qd)
        {
            if (values == null)
                values = new List<IndicatorValue>();
            if (values.Count != 0 && qd.Date < values.Last().OpenTime)
                throw new TimeoutException("Couldn't insert quotedata");
        }

        public override void Update(QuoteData qd)
        {
            validate(qd);
            addValue(qd, getCalculatedValue(qd));
        }
    }
}
